package com.yanqu.road.server.manger.config;

import com.yanqu.road.dao.impl.config.TopicLinkConfigDaoImpl;
import com.yanqu.road.entity.activity.topiclink.TopicLinkConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.topiclink.TopicLinkModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 同步圈子链接
 */

public class TopicLinkMgr extends TempMgr {

    private static Map<String, List<TopicLinkConfig>> topicLinkConfigMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return reloadConfigData();
    }

    public static boolean reloadConfigData(){
        getLogger().info("reload topicLinkConfigMap start");
        TopicLinkConfigDaoImpl topicLinkConfigDao = new TopicLinkConfigDaoImpl();
        Map<String, List<TopicLinkConfig>> tempMap = topicLinkConfigDao.getTopicLinkConfigMap();
        if (tempMap != null) {
            topicLinkConfigMap = tempMap;
            for (GamePlayer player: GamePlayerMgr.getAllOnlinePlayer()){
                player.getModule(TopicLinkModule.class).syncTopicLinkConfig();
            }
            getLogger().info("reload topicLinkConfigMap over");
        }else {
            getLogger().info("reload topicLinkConfigMap empty");
        }

        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public Map<String, List<TopicLinkConfig>> getTopicLinkConfigMap() {
        return topicLinkConfigMap;
    }

    public static List<TopicLinkConfig> getTopicLinkConfigList(String topicId){
        if (topicLinkConfigMap == null){
            return new ArrayList<>();
        }
        return topicLinkConfigMap.get(topicId);
    }
}
